package jinyilw.tools.audio;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.Player;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;

public class Mp3Renderer extends BaseAudioRenderer
{
	private Player Mp3player;

	public Mp3Renderer()
	{
	}

	public boolean isAvailable()
	{
		return true;
	}

	protected void playSound(final URL audioFile)
	{
		Thread thread = new Thread(() -> {
			try
			{
				Mp3Renderer.this.Mp3player = new Player(
						new BufferedInputStream(audioFile.openStream()),
						FactoryRegistry.systemRegistry().createAudioDevice());
				Mp3Renderer.this.Mp3player.play();
			} catch (IOException var2)
			{
				Mp3Renderer.this.status = 4;
				System.err.println("Can not load audioFile (" + audioFile + ": " + var2);
			} catch (JavaLayerException var3)
			{
				Mp3Renderer.this.status = 4;
				System.err.println("Problem playing audio: " + var3);
			}

		});
		thread.setDaemon(true);
		thread.start();
	}

	protected void replaySound(URL audioFile)
	{
		this.playSound(audioFile);
	}

	protected void stopSound()
	{
		if (this.Mp3player != null)
		{
			this.Mp3player.close();
			this.Mp3player = null;
		}

	}

	public int getStatus()
	{
		if (this.Mp3player != null)
		{
			return this.Mp3player.isComplete() ? 3 : super.getStatus();
		} else
		{
			return super.getStatus();
		}
	}

	public boolean isVolumeSupported()
	{
		return false;
	}
}